package com.app.config;

import com.app.entity.User;
import com.app.service.UserService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component
public class InitData implements CommandLineRunner {
    @Resource
    private UserService userService;

    @Value("${user.username}")
    private String name;
    @Value("${user.password}")
    private String passwd;

    @Override
    public void run(String... args) {
        log.info("开始初始化数据");
        initUser();
    }

    private void initUser() {
        if (!userService.exists(Wrappers.<User>lambdaQuery().eq(Objects.nonNull(name), User::getUsername, name))) {
            userService.save(new User(name, passwd));
        }
    }
}
